﻿using MESPro.DBManager;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MESPro.Models;
using Mapster;
using System.Xml.Linq;
using SqlSugar;

namespace MESPro.Views
{
    public partial class AddFunctionGroup : BaseAddFrm
    {
        #region Public Constructors

        public AddFunctionGroup()
        {
            InitializeComponent();
        }

        public AddFunctionGroup(Model fg)
        {
            InitializeComponent();
            this.fg = fg;
            this.Load += AddFunctionGroup_Load;

        }

        #endregion Public Constructors

        #region 字段

        public List<FunctionGroup> src = new();
        public List<FunctionGroup> dic = new();
        private readonly Model? fg;

        #endregion 字段

        #region Private 方法

        private void AddFunctionGroup_Load(object sender, EventArgs e)
        {
            if (fg == null) return;
            var noadd = DbContext.Db.Queryable<FunctionGroup>()
                             .Where(x => !fg.FunctionGroups.Any(y => y.Id == x.Id))
                             .ToList();
            dic = fg.FunctionGroups;
            src = noadd.Adapt<List<FunctionGroup>>();
            dic = fg.FunctionGroups;


            this.listBox_src.DataSource = src;
            this.listBox_dic.DataSource = dic;
            listBox_src.DisplayMember = nameof(FunctionGroup.DisName);
            listBox_src.ValueMember = "Id";

            listBox_dic.DisplayMember = nameof(FunctionGroup.DisName);
            listBox_dic.ValueMember = "Id";
         
         
        }

        private void 确定(object sender, EventArgs e)
        {
            //todo 由于是一对多得关系,删除会导致直接删除FunctionGroup,而不是删除关系
            fg.FunctionGroups = dic;
            List<Model> List = new List<Model>() { fg };
            var res = DbContext.Db.UpdateNav(List)
                         .Include(z1 => z1.FunctionGroups)
                         .ExecuteCommand();
            MainServerFrm.RMShowMsg?.Invoke(res ? "ok" : "ng", Enums.MsgLevel.Trace);
            this.Close();
        }

        private void 左边移动到右边(object sender, EventArgs e)
        {
            var s = this.listBox_src.SelectedItems.Cast<FunctionGroup>().ToList();//要添加的
            var d = this.listBox_dic.SelectedItems.Cast<FunctionGroup>().ToList();//要删除的
            foreach (var x in s)
            {
                src.Remove(x);
                dic.Add(x);
            }
            this.listBox_src.DataSource = null;
            this.listBox_dic.DataSource = null;
            this.listBox_src.DataSource = src;
            this.listBox_dic.DataSource = dic;
            listBox_src.DisplayMember = nameof(FunctionGroup.DisName);
            listBox_src.ValueMember = "Id";

            listBox_dic.DisplayMember = nameof(FunctionGroup.DisName);
            listBox_dic.ValueMember = "Id";
        }

        private void 右边移动到左边(object sender, EventArgs e)
        {
            var s = this.listBox_src.SelectedItems.Cast<FunctionGroup>().ToList();//要添加的
            var d = this.listBox_dic.SelectedItems.Cast<FunctionGroup>().ToList();//要删除的
            foreach (var x in d)
            {
                dic.Remove(x);
                src.Add(x);
            }
            this.listBox_src.DataSource = null;
            this.listBox_dic.DataSource = null;
            this.listBox_src.DataSource = src;
            this.listBox_dic.DataSource = dic;
            listBox_src.DisplayMember = nameof(FunctionGroup.DisName);
            listBox_src.ValueMember = "Id";

            listBox_dic.DisplayMember = nameof(FunctionGroup.DisName);
            listBox_dic.ValueMember = "Id";
        }

        #endregion Private 方法
    }
}
